<template>
  <div class="">
    <el-card>
      <el-table :data="allRoles" border style="width: 100%">
        <el-table-column :label="$t('msg.role.index')" type="index" width="120">
        </el-table-column>
        <el-table-column :label="$t('msg.role.name')" prop="title">
        </el-table-column>
        <el-table-column :label="$t('msg.role.desc')" prop="describe">
        </el-table-column>
        <el-table-column :label="$t('msg.role.action')" prop="action" width="260">
          <template #default="{ row }">
            <!-- 分配权限 -->
            <el-button type="primary" size="mini" @click="onDistributePermissionClick(row)"
              v-permission="['distributePermission']">
              {{ $t('msg.role.assignPermissions') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 为角色分配权限 -->
    <distribute-permission v-model="distributePermissionVisible" :roleId="selectRoleId"></distribute-permission>
  </div>
</template>

<script setup>
import { roleList } from '@/api/role'
import DistributePermission from './components/DistributePermission.vue'
// import { watchSwitchLang } from '@/utils/i18n'
import { ref } from 'vue'

/**
 * 获取角色列表
 */
const allRoles = ref([])
const getRoleList = async () => {
  allRoles.value = await roleList()
}
getRoleList()
// TODO 监听语言切换 后重新获取数据列表
// watchSwitchLang(getRoleList)

/**
 * 给角色分配权限
 */
const distributePermissionVisible = ref(false)
const selectRoleId = ref('') // 当前角色id  
const onDistributePermissionClick = row => {
  distributePermissionVisible.value = true
  selectRoleId.value = row.id
}
</script>

<style lang="scss" scoped></style>
